package org.testis.testdriver.operations;

import org.testis.testdriver.*;
import org.testis.data.TestStepDataObject;
import org.testis.services.TestStepExecutor;
import org.testis.testdriver.AtofEnumaration.DebuggingMode;
import java.util.LinkedHashMap;

/**
 *
 * @author barisergun
 */
public class TestPartOperationEndTestPlan extends TestPartOperationDecorator {

    private TestPart partInUse;

    public TestPartOperationEndTestPlan(TestPart testPart) {
        partInUse = testPart;
    }

    @Override
    public boolean runOperation(DebuggingMode mode) throws InterruptedException {
        if (super.runOperation(mode)) {
            return runSpecificBehaviour();
        } else {
            return false;
        }
    }

    /**
     * This method is used to drive the specific Tasks that belong to this decorator
     * operation.
     * 
     * @return boolean the result of operation. This result will affect the operation
     * of linked decorator tasks. 
     */
    private boolean runSpecificBehaviour() {
        if (((StepContainer) partInUse).getSerialConnData() != null) {
            if (!((StepContainer) partInUse).getSerialConnData().isEmpty()) {

                TestStepDataObject myData = new TestStepDataObject();
                myData.setMessageId(partInUse.getPartGuid());
                myData.setStoreResultPostfix(partInUse.getStoreResultPostfix());
                myData.setTestStepId(partInUse.getPartId());
                myData.setModuleType(AtofEnumaration.AtofModule.SerialPortModule.name());
                myData.setActionType(AtofEnumaration.AtofSerialPortModuleAction.EndConnection.name());
                myData.setMacAddress(((StepContainer) partInUse).getTestPcMacAddr());
                myData.setAsynchCall(false);
                myData.setTestStepDataName("Stop Listening Serial Connection");
                myData.setTestStepDataId(partInUse.getPartGuid());
                myData.setStopExecution(true);
                LinkedHashMap<String, String> cloneOfMap =
                        new LinkedHashMap<String, String>();
                cloneOfMap.putAll(
                        ((StepContainer) partInUse).getSerialConnData());
                myData.setDataToApply(cloneOfMap);
                TestStepExecutor executor = new TestStepExecutor();
                executor.send(myData,
                        true);

                TestStepExecutor.getResultReciever(
                        true).recieve(
                        partInUse.getPartGuid(),
                        10
                        * 1000);
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException ex) {
                }



            }
        }

        return true;
    }
}
